package com.ncloud.act.service.impl;

import java.text.ParseException;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSONObject;
import com.ncloud.param.service.SequenceService;
import com.ncloud.act.common.ActDataHelper;
import com.ncloud.act.entity.ChnlAdjustBill;
import com.ncloud.act.entity.ChnlBill;
import com.ncloud.act.mapper.BsCycleMapper;
import com.ncloud.act.mapper.ChnlAdjustBillMapper;
import com.ncloud.act.mapper.ChnlBillMapper;
import com.ncloud.act.entity.BsCycle;
import com.ncloud.common.exception.UserException;

@Service
public class ChnlAdjustBillTransServiceImpl{
	@Resource
	private BsCycleMapper bsCycleMapper;
	@Resource
	private ChnlBillMapper chnlBillMapper;
	@Resource
	private ChnlAdjustBillMapper chnlAdjustBillMapper;
	@Reference
	private SequenceService sequenceService;
	@Transactional(propagation=Propagation.REQUIRED)
	public JSONObject insertChnlAdjustBill(JSONObject json) throws ParseException{
		Long total_id           =0l;
		String adjust_type        ="1";
		Long adjust_fee          =0l;
		Long recv_user_id      =0l;
		String remark      ="";
		if(json.containsKey("total_id"))
		{
			total_id=json.getLong("total_id");
		}else{
			throw new UserException("4000", "没有渠道编码");
		}
		if(json.containsKey("adjust_type"))
		{
			//0-调增 1-调减
			adjust_type=json.getString("adjust_type");
		}else{
			throw new UserException("4000", "没有渠道编码");
		}
		if(json.containsKey("adjust_fee"))
		{
			Double dFee=ActDataHelper.toDouble(json.getString("adjust_fee"));
			adjust_fee=(long)(dFee*100);
		}else{
			throw new UserException("4000", "没有缴费金额");
		}
		if(json.containsKey("userID"))
		{
			recv_user_id=json.getLong("userID");
		}
		if(json.containsKey("remark"))
		{
			remark=json.getString("remark");
		}
		//当前账期
		List<BsCycle> currentCycle = bsCycleMapper.getCurrentCycle();
		if (currentCycle.size()==0){return json;}
		Integer current_cycle_id=currentCycle.get(0).getCycle_id();
		List<ChnlBill> chnlBills = chnlBillMapper.getChnlBillByTotalID(total_id);
		if(chnlBills==null||chnlBills.size()==0){
			throw new UserException("4000", "调账的账单不存在");
		}
		ChnlBill chnlBill=chnlBills.get(0);
		ChnlAdjustBill chnlAdjustBill = new ChnlAdjustBill();
		Long adjust_id=sequenceService.getSequence("adjust_id");
		chnlAdjustBill.setAdjust_id(adjust_id);
		chnlAdjustBill.setTotal_id(chnlBill.getTotal_id());
		chnlAdjustBill.setChnl_id(chnlBill.getChnl_id());
		chnlAdjustBill.setChnl_name(chnlBill.getChnl_name());
		chnlAdjustBill.setBill_cycle_id(chnlBill.getCycle_id());
		chnlAdjustBill.setAdjust_cycle_id(current_cycle_id);
		chnlAdjustBill.setAdjust_type(adjust_type);//0-调增 1-调减
		if("1".equals(adjust_type)){
			adjust_fee=-adjust_fee;
		}
		chnlAdjustBill.setAdjust_fee(adjust_fee);
		chnlAdjustBill.setAdjust_balance(adjust_fee);
		chnlAdjustBill.setPay_tag("0");//0-初始状态 1-未冲正 2-已冲正 3-部分冲正
		chnlAdjustBill.setCreate_time(new Date());
		chnlAdjustBill.setCreate_user_id(recv_user_id);
		chnlAdjustBill.setUpdate_time(new Date());
		chnlAdjustBill.setUpdate_user_id(recv_user_id);
		chnlAdjustBill.setRemark(remark);
		chnlAdjustBillMapper.insertChnlAdjustBill(chnlAdjustBill);
		return json;
	}
}
